/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package clientFrame;

import views.Status;
import ejb.sessions.TDSessionRemote;
import views.ComboBoxCellRender;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import models.*;


/**
 *
 * @author simone
 */
public class CreateFrame extends javax.swing.JFrame {

    private final DefaultListModel listTourModel;
    private final DefaultListModel listHotelModel;
    private final DefaultListModel listTravelModel;

    private errorDialog errDialog;
    Status status;
    
    @SuppressWarnings("FieldMayBeFinal")
    private PacchettoDTO selectedPacket;
    private ArrayList<hotelDTO> hotels =null;
    private ArrayList<stanzaDTO> rooms =null;
    private ArrayList<tourDTO> tour =null;
    private ArrayList<TripDTO> trip =null;
    private ArrayList<hotelDTO> selectedHotels = new ArrayList<>();
    private ArrayList<tourDTO> selectedTour = new ArrayList<>();
    private ArrayList<TripDTO> selectedTrip = new ArrayList<>();

    public CreateFrame(Status status,PacchettoDTO packet) 
    {
        initComponents();
        listHotelModel = new DefaultListModel();
        listTourModel = new DefaultListModel();
        listTravelModel = new DefaultListModel();
        
        this.status = status;
        this.selectedPacket = packet;
        
        hotelList.setModel(listHotelModel);
        tourList.setModel(listTourModel);
        travelList.setModel(listTravelModel);
 
        fillHotelComboBox();
        fillRoomComboBox();
        fillTourComboBox();
        fillTravelComboBox();
        setCellRender();

        if(status==status.modify)
        {
            fillAllTableList();
            filltextField();
            createPackBtn.setText("Aggiorna");
        }    
    }
    
    
    private void fillAllTableList()
    {
        int i;
        selectedHotels = selectedPacket.getSelectedHotels();
        for (i = 0; i < selectedHotels.size(); i++) {
            listHotelModel.addElement(selectedHotels.get(i).getInfo());
        }
        
        selectedTour = selectedPacket.getSelectedTours();
        for (i = 0; i < selectedTour.size(); i++) {
            listTourModel.addElement(selectedTour.get(i).getNameTour());
        }
        
        selectedTrip = selectedPacket.getSelectedTrip();
        for (i = 0; i < selectedTrip.size(); i++) {
            listTravelModel.addElement(selectedTrip.get(i).getInfo());
        } 
    }
    
    private void filltextField()
    {
        String name = selectedPacket.getName();
        String desc = selectedPacket.getDescription();
        Date dateDal = selectedPacket.getDal();
        Date dateAl = selectedPacket.getal();
        stanzaDTO room = selectedPacket.getRoom();
        nameField.setText(name);
        descrText.setText(desc);

        dateALFormattedField.setValue(dateAl);
        dateDALformattedField.setValue(dateDal);
        stanzaBox.setSelectedItem(room);
        
        int onSale = selectedPacket.getOnSale();
        int predef = selectedPacket.getPredef();
        
        onSaleCheckBox.setSelected(convertIntToBoolean(onSale));
        predefCheckBox.setSelected(convertIntToBoolean(predef));
    }
    
    private boolean convertIntToBoolean(int intValue) {
        return (intValue != 0);
    }

    private void setCellRender()
    {
        ComboBoxCellRender cellRender = new ComboBoxCellRender();
        if(!hotels.isEmpty())
            hotelBox.setRenderer(cellRender);
        if(!tour.isEmpty())
            toursBox.setRenderer(cellRender);
        if(!trip.isEmpty())
            travelsBox.setRenderer(cellRender);      
        if(!rooms.isEmpty())
            stanzaBox.setRenderer(cellRender);
    }

   private void fillHotelComboBox()
   {
       TDSessionRemote session = lookupTDSessionRemote();
       this.hotels = session.getAllHotels();
       for (hotelDTO hDTO : hotels) {
           hotelBox.addItem(hDTO);
       }
   }
   
   
   private void fillRoomComboBox()
   {
       TDSessionRemote session = lookupTDSessionRemote();
       this.rooms = session.getAllRooms();
       for (stanzaDTO sDTO : rooms) {
           stanzaBox.addItem(sDTO);
       }
   }
   
   private void fillTourComboBox()
   {
       TDSessionRemote session = lookupTDSessionRemote();
       this.tour = session.getAllTour();
       for (tourDTO tDTO : tour) {
           toursBox.addItem(tDTO);
       }
   }
       
   private void fillTravelComboBox()
   {
       TDSessionRemote session = lookupTDSessionRemote();
       this.trip = session.getAllTrip();
       for (TripDTO trDTO : trip) {
           travelsBox.addItem(trDTO);
       }
   }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane5 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nameField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        descrText = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        predefCheckBox = new javax.swing.JCheckBox();
        onSaleCheckBox = new javax.swing.JCheckBox();
        createPackBtn = new javax.swing.JButton();
        hotelBox = new javax.swing.JComboBox();
        travelsBox = new javax.swing.JComboBox();
        toursBox = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        travelList = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        tourList = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        hotelList = new javax.swing.JList();
        jLabel6 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        dateALFormattedField = new javax.swing.JFormattedTextField();
        dateDALformattedField = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        stanzaBox = new javax.swing.JComboBox();
        errLabel = new javax.swing.JLabel();

        jScrollPane5.setViewportView(jTextPane1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("create packet");

        jLabel1.setText("Nome:");

        jLabel2.setText("Descrizione:");

        descrText.setColumns(20);
        descrText.setLineWrap(true);
        descrText.setRows(5);
        descrText.setWrapStyleWord(true);
        jScrollPane1.setViewportView(descrText);

        jLabel3.setText("Hotels:");

        jLabel4.setText("Tours:");

        jLabel5.setText("Travels:");

        predefCheckBox.setText("Predefinito");

        onSaleCheckBox.setText("on sale");

        createPackBtn.setText("Create");
        createPackBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createPackBtnActionPerformed(evt);
            }
        });

        hotelBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hotelBoxActionPerformed(evt);
            }
        });

        travelsBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                travelsBoxActionPerformed(evt);
            }
        });

        toursBox.setToolTipText("");
        toursBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toursBoxActionPerformed(evt);
            }
        });

        travelList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                travelListKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(travelList);

        tourList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tourListKeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(tourList);

        hotelList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                hotelListKeyPressed(evt);
            }
        });
        jScrollPane4.setViewportView(hotelList);

        jLabel6.setText("Disponibilità:");

        dateALFormattedField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/20yy"))));

        dateDALformattedField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/20yy"))));

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("dal");

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setText("al");

        jLabel9.setText("Tipo stanza:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(predefCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(onSaleCheckBox))
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSeparator1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE)))
                            .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(dateDALformattedField)
                            .addComponent(dateALFormattedField, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                            .addComponent(stanzaBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(errLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(toursBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(hotelBox, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(jLabel5)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(travelsBox, javax.swing.GroupLayout.PREFERRED_SIZE, 493, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(createPackBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(hotelBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(toursBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(travelsBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(8, 8, 8)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(dateDALformattedField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dateALFormattedField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(stanzaBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(predefCheckBox)
                            .addComponent(onSaleCheckBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(createPackBtn)
                    .addComponent(errLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

 
   // private final ArrayList<hotelDTO> selectedHotels = new ArrayList<>();

    private void hotelBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hotelBoxActionPerformed
        // TODO add your handling code here:
        
        JComboBox cb = (JComboBox)evt.getSource();
        int index = cb.getSelectedIndex();
        if(cb.isPopupVisible())
        {
            hotelDTO hotel = (hotelDTO)cb.getSelectedItem();
            listHotelModel.addElement(hotel.getInfo());
            selectedHotels.add(hotels.get(index));
        }
    }//GEN-LAST:event_hotelBoxActionPerformed

    
    private PacchettoDTO InitializedPacket()
    {
        String name = nameField.getText();
        String descr = descrText.getText();
        int onSale = onSaleCheckBox.isSelected() ? 1 : 0;
        int predef = predefCheckBox.isSelected() ? 1 : 0;

        Date dal = (Date) dateDALformattedField.getValue();
        Date al = (Date) dateALFormattedField.getValue();
        
        stanzaDTO room = (stanzaDTO) stanzaBox.getSelectedItem();
        
        if(name.isEmpty() || descr.isEmpty())
            return null;
       
        if(dal.compareTo(al)>=0)
            return null;
        
        if(dal==null || al==null)
            return null;
        
        PacchettoDTO packet = new PacchettoDTO();
        packet.setPacchettoDTO(name, descr, predef, onSale);
        packet.setRoom(room);
        packet.setDates(dal, al);
        packet.setHotels(selectedHotels);
        packet.setTours(selectedTour);
        packet.setTrip(selectedTrip);
        packet.setIdPacchetto(selectedPacket.getIdPacchetto());
        return packet;
    }

    private void createPackBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createPackBtnActionPerformed
        // TODO add your handling code here:
        boolean created = false;
        TDSessionRemote session = lookupTDSessionRemote();
        PacchettoDTO packet = InitializedPacket();
        
        if(packet==null)
        {
            errLabel.setText("missing field or wrong date");
            return;
        }
        errLabel.setText("");
        if(status==Status.create)
            created = session.createPacket(packet);
        else
            created = session.updatePacket(packet);
        
        if(created)
        {
            this.setVisible(false);
            this.dispose();
        }
        else
        {
            errDialog = new errorDialog(this, true);
            errDialog.setVisible(true);
        }        
    }//GEN-LAST:event_createPackBtnActionPerformed

    private void toursBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toursBoxActionPerformed
        // TODO add your handling code here:
        JComboBox cb = (JComboBox) evt.getSource();
        int index = cb.getSelectedIndex();
        if (cb.isPopupVisible()) 
        {
            tourDTO tours = (tourDTO) cb.getSelectedItem();
            listTourModel.addElement(tours.getNameTour());
            selectedTour.add(tour.get(index));
        }
        
    }//GEN-LAST:event_toursBoxActionPerformed

    private void travelsBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_travelsBoxActionPerformed
        // TODO add your handling code here:
        JComboBox cb = (JComboBox) evt.getSource();
        int index = cb.getSelectedIndex();
        if (cb.isPopupVisible()) 
        {
            TripDTO travel = (TripDTO) cb.getSelectedItem();
            listTravelModel.addElement(travel.getInfo());
            selectedTrip.add(trip.get(index));
        }
    }//GEN-LAST:event_travelsBoxActionPerformed

    private void hotelListKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_hotelListKeyPressed
        // TODO add your handling code here:
        JList list  = (JList)evt.getSource();
        int index = list.getSelectedIndex();
        if(index<0)
            return;
        
        selectedHotels.remove(index);
        listHotelModel.remove(index);
        hotelList.validate();
    }//GEN-LAST:event_hotelListKeyPressed

    private void tourListKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tourListKeyPressed
        // TODO add your handling code here:
        JList list = (JList) evt.getSource();
        int index = list.getSelectedIndex();
        if (index < 0) {
            return;
        }

        selectedTour.remove(index);
        listTourModel.remove(index);
        tourList.validate();
    }//GEN-LAST:event_tourListKeyPressed

    private void travelListKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_travelListKeyPressed
        // TODO add your handling code here:
        JList list = (JList) evt.getSource();
        int index = list.getSelectedIndex();
        if (index < 0) {
            return;
        }
        selectedTrip.remove(index);
        listTravelModel.remove(index);
        travelList.validate();
    }//GEN-LAST:event_travelListKeyPressed
/**/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton createPackBtn;
    private javax.swing.JFormattedTextField dateALFormattedField;
    private javax.swing.JFormattedTextField dateDALformattedField;
    private javax.swing.JTextArea descrText;
    private javax.swing.JLabel errLabel;
    private javax.swing.JComboBox hotelBox;
    private javax.swing.JList hotelList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTextField nameField;
    private javax.swing.JCheckBox onSaleCheckBox;
    private javax.swing.JCheckBox predefCheckBox;
    private javax.swing.JComboBox stanzaBox;
    private javax.swing.JList tourList;
    private javax.swing.JComboBox toursBox;
    private javax.swing.JList travelList;
    private javax.swing.JComboBox travelsBox;
    // End of variables declaration//GEN-END:variables

    private TDSessionRemote lookupTDSessionRemote() {
        try {
            Context c = new InitialContext();
            return (TDSessionRemote) c.lookup("java:comp/env/TDSession");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
